//
//  EAFoodDetailController.m
//  WhatToEat
//
//  Created by 方力 on 15/11/24.
//  Copyright (c) 2015年 Mac. All rights reserved.
//

#import "EAFoodDetailController.h"
#import "CommonTableView.h"
#import "EACollectionViewCell.h"
#import "EARoundMealController.h"
#import "EAfoodDetailCell.h"

@interface EAFoodDetailController ()<UITableViewDataSource,UITableViewDelegate>
@property(strong,nonatomic)CommonTableView * tableView;
@property(strong,nonatomic)NSDictionary * mealDict;
@end

@implementation EAFoodDetailController

- (void)viewDidLoad {
    [super viewDidLoad];

    

    self.edgesForExtendedLayout=UIRectEdgeTop;

    self.navigationController.navigationBar.hidden=YES;
    
    self.tableView=[[CommonTableView alloc] initWithFrame:CGRectMake(0, 0, kUI_SCREEN_WIDTH, kUI_SCREEN_HEIGHT-self.tabBarController.tabBar.height)];
    [self.view addSubview:self.tableView];
    
    self.tableView.delegate=self;


    
  
    
    UIImageView * iv=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kUI_SCREEN_WIDTH, kUI_SCREEN_WIDTH*2/3)];
                      
    [iv setImageWithURL:[NSURL URLWithString:self.foodDict[@"img"]]];
    self.tableView.tableHeaderView=iv;

    
    UILabel * l=[[UILabel alloc] initWithFrame:CGRectMake(0, iv.height-38, kUI_SCREEN_WIDTH, 38)];
    l.text=[NSString stringWithFormat:@"     %@",self.foodDict[@"name"]];
    l.textColor=[UIColor whiteColor];
    [iv addSubview:l];
    l.alpha=0.5;
    l.backgroundColor=[UIColor blackColor];
    l.font=kFont(18);
    l.textAlignment=0;
    
    
    
    
    
    
    
    
    UIButton * backBtn=[[UIButton alloc] init];
    
    
    [self.view addSubview:backBtn];
    
    [backBtn setImage:[UIImage imageNamed:@"back"] forState:0];
    
    [backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.view.mas_top).with.offset(28);
        
        make.left.mas_equalTo(self.view.mas_left).with.offset(10);
        
        make.size.mas_equalTo(CGSizeMake(44, 33));
        
        
    }];
    
    backBtn.tag=1;
    
    [backBtn addTarget:self action:@selector(toSaveAction) forControlEvents:UIControlEventTouchUpInside];
    
    
    UILabel * titleLable=[[UILabel alloc] init];
    
    titleLable.font=kFont(20);
    
    titleLable.textColor=[UIColor whiteColor];
    titleLable.text=@"菜品详情";
    [self.view addSubview:titleLable];
    
    
    [titleLable mas_makeConstraints:^(MASConstraintMaker *make) {
        
        
        make.top.mas_equalTo(self.view.mas_top).with.offset(28);
        
        make.centerX.mas_equalTo(self.view);
        
        
        
    }];
    
    
    
    [self upDAteDataSouth];
    
    
    
    
    
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    
    EAfoodDetailCell * cell=(EAfoodDetailCell *)[tableView cellForRowAtIndexPath:indexPath];
    
    if (indexPath.row==2) {
        return 120;
    }
    
    if (indexPath.row==3) {
        return 100;
    }
    
    
    
    
    
    return 44;

}


-(void)toSaveAction
{

    [self.navigationController popViewControllerAnimated:YES];

}

-(void)viewWillDisappear:(BOOL)animated
{

    [super viewWillDisappear:animated];
    
    
    self.navigationController.navigationBar.hidden=NO;


}


-(void)upDAteDataSouth
{
    
    [WebAPI webAPISignAddMd5tWithNote:@"user/mealDetail" parm:@{
                                                                @"mealID":self.foodDict[@"id"]
                                                                
                                                                } completed:^(NSInteger state, AFHTTPRequestOperation *operation, id responseObject) {
       
        
                                                                    self.mealDict=responseObject[@"meal"];
        
        
    }];
    

}


-(void)setMealDict:(NSDictionary *)mealDict
{
    _mealDict=mealDict;
    
    [self.tableView addNibWithEntity:@{
                                       @"1":@"",@"2":@""
                                       
                                       } andCellName:@"EAfoodDetailCell"];
    

        
        
        

            
            [self.tableView addNibWithEntity:@{
                                               @"1":@"卡路里",@"2":[NSString stringWithFormat:@"%@卡",mealDict[@"energy"]]
                                               
                                               } andCellName:@"EAfoodDetailCell"];
            
            

        

            
            [self.tableView addNibWithEntity:@{
                                               @"1":@"功效",@"2":[NSString stringWithFormat:@"%@",mealDict[@"slgx"]]
                                               
                                               } andCellName:@"EAfoodDetailCell"];
            
            
    
            
            [self.tableView addNibWithEntity:@{
                                               @"1":@"适宜人群",@"2":[NSString stringWithFormat:@"%@",mealDict[@"syrq"]]
                                               
                                               } andCellName:@"EAfoodDetailCell"];
            
    UIView * footerView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, kUI_SCREEN_WIDTH, 80)];
    self.tableView.tableFooterView=footerView;
    
    
    EAMButton * bt=[[EAMButton alloc] init];

    [bt setTitle:@"加入餐桌" forState:0];
    
    bt.frame=CGRectMake(kUI_SCREEN_WIDTH/4, 30, kUI_SCREEN_WIDTH/2, 34);
    
    [footerView addSubview:bt];

    if ([self.selectFoods containsObject:self.foodDict]) {
        
        
        bt.backgroundColor=[UIColor grayColor];
        bt.userInteractionEnabled=NO;
        
        
        
    }
    
    bt.rac_command=[[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
       

        

        
        @try {
                UIViewController * vc=self.navigationController.viewControllers[self.navigationController.viewControllers.count-2];
            
            if ([vc isKindOfClass:[EARoundMealController class]]) {
                
                EARoundMealController * rVC=(EARoundMealController *)vc;
                
                
                
                
                [rVC.selectFoods addObject:self.mealDict];

            }
            
        }
        @catch (NSException *exception) {
            
        }
        @finally {
            
        }

        
        
        [self.navigationController popViewControllerAnimated:YES];
        
        
        
           return [RACSignal empty];
    }];

 
    



    
    
    


}





@end
